$(function () {
    //设置场景图位置到中间
    setViewImgPosition();
    function setViewImgPosition() {
        var clientWidth=document.body.clientWidth;
        var imgW=$('.main-view-image').width();
        console.log(clientWidth,imgW);
        var distance=(imgW-clientWidth)/2;
        $('.swiper-slide-inner').scrollLeft(distance);
    }
    //第几个场景1~4
    var VIEW = 1;
    //轮播四个场景图
    var mySwiper = new Swiper('#main-swiper', {
        autoplay: false,//可选选项，自动滑动
        lazyLoading : true,
        loop:true,
        pagination: {
            el: '.swiper-pagination',
        },
        on: {
            slideChange: function () {//切换场景时
                if(!mySwiper) {
                    return false;
                }else{
                    changeViewName(mySwiper.activeIndex);//改变场景名称
                    var nowImg=$('.main-view-image').eq(mySwiper.activeIndex);
                    var src=nowImg.attr('src');
                    var dataSrc=nowImg.attr('data-src');
                    if(src!==dataSrc){
                        nowImg.attr('src',dataSrc);
                    }
                };
            }
        }
    });
    //左右切换
    $('#prev').click(function () {
        mySwiper.slidePrev();
    });
    $('#next').click(function () {
        mySwiper.slideNext();
    });


    var audio = null;
    //播放和暂停音乐
    document.addEventListener("WeixinJSBridgeReady", function () {
        audioInit();
    }, false);


    function audioInit() {
        audio = new Audio();
        audio.loop = true;
        audio["src"] = 'img/Happy New Year.lite.mp3';
        audio.load();
        audio.play();
    };

    $('.bg-music').click(function () {
       if($(this).hasClass('music-pause')){
           $(this).removeClass('music-pause').addClass('music-play');
       } else if($(this).hasClass('music-play')){
           $(this).removeClass('music-play').addClass('music-pause');
       }
        playOrPauseMusic();
    });

    //关闭弹窗
    closeMask();
    //点击发光点
    clickDots();
    //底部消息
    ScrollImgLeft();
    //点击导航
    clickTopNav();
    //拖动场景图
    dragView();
    //拖动场景图
    function dragView() {
        var obj=$('.main-view-image-bg');
        obj.bind('touchend',function () {
            $('.main-gesture-wrap').hide();//隐藏手势
        });
    }


    //关闭弹窗
    function closeMask() {
        //点击弹窗叉号和继续寻宝都关闭弹窗
        $('.dialog-close-btn,.btn-close').bind('click', function () {
            $('.mask').hide();
            if($(this).attr('data-refresh')==1){//关注弹窗关闭需要刷新
                window.location.replace='main.html';
            }
        });
        //首次进入显示开始寻宝弹窗
        var init=window.sessionStorage.init;
        console.log('window.sessionStorage.init:',init);
        if(init==undefined){
            $('#startMask').show();
            window.sessionStorage.init=1;
        }
        //点击屏幕开始寻宝弹窗消失
        $('#startMask').click(function () {
            $(this).hide();
        });
    }

    //切换场景名称
    function changeViewName(num) {
        VIEW = num + 1;
        var arr = ['皇家大道', '甲板层及北极星', '海上多功能运动馆', '甲板冲浪及甲板跳伞'];
        $('#location-name').text(arr[num]);
        $('.main-gesture-wrap').hide();
    }

    //创建图上分布的点
    function createDot(container) {
        var html = '';
        for (var i = 0; i < 10; i++) {
            var top = Math.floor(Math.random() * 40) + 30 + '%';
            var left = Math.floor(Math.random() * 80) + 10 + '%';
            var dot = '<div class="dot-container" style="left:' + left + ';top:' + top + '">\n' +
                '<div class="pr dot-container-inner">' +
                '<div class="dot-circle"></div>' +
                '<div class="dot center"></div>' +
                '</div>' +
                '                    </div>';
            html = html + dot;
        }
        container.append(html);
    }

    //显示图上分布的点
    function showDots() {
        var obj = $('.main-view-image-bg');
        for (var k = 0; k < obj.length; k++) {
            createDot(obj.eq(k));
        }
    }

    //点击发光点
    function clickDots() {
        showDots();
        $('.dot-container').bind('click', function () {
            $('#alert-login').show();
        })

    }

    //文字横向滚动
    function ScrollImgLeft() {
        var speed = 20;
        var MyMar = null;
        var scroll_begin = document.getElementById("scroll_begin");
        var scroll_end = document.getElementById("scroll_end");
        var scroll_div = document.getElementById("scroll_div");
        scroll_end.innerHTML = scroll_begin.innerHTML;

        function Marquee() {
            if (scroll_end.offsetWidth - scroll_div.scrollLeft <= 0)
                scroll_div.scrollLeft -= scroll_begin.offsetWidth;
            else
                scroll_div.scrollLeft++;
        }

        MyMar = setInterval(Marquee, speed);
        scroll_div.onmouseover = function () {
            clearInterval(MyMar);
        }
        scroll_div.onmouseout = function () {
            MyMar = setInterval(Marquee, speed);
        }
    }

    //音乐暂停与播放
    function playOrPauseMusic() {
        audio.paused ? audio.play() : audio.pause();
    }


    function clickTopNav() {
        $('.main-nav-item1').click(function () {
            //没有中一等奖
            // $('#mask-1-0').show();
            //中一等奖
            $('#mask-1-1').show();
        })
        $('.main-nav-item2').click(function () {
            //没有中二等奖
            // $('#mask-2-0').show();
            //中二等奖
            $('#mask-2-1').show();
        })
        $('.main-nav-item4').click(function () {
            //没有中三等奖
            $('#mask-3-0').show();
            //中三等奖
            // $('#mask-3-1').show();
        })
        $('.main-nav-item5').click(function () {

            $('#mask-4-1').show();
        })
    }
})
